﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using NextCMS.Models;

public partial class LayoutMode : WebadminBasePage
{
    public string fileName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        TopFixedMenu1.UserLanguageId = UserLanguageId;
        TopFixedMenu1.FrontSiteId = FrontSiteId;
        TopFixedMenu1.IsLayoutMode = true;
        fileName=Convert.ToString(Page.RouteData.Values["fileName"]);
        if (!String.IsNullOrEmpty(fileName))
        {
            PagesHelper pc = new PagesHelper();
            PageModel pm= pc.GetPageByFileName(fileName,FrontSiteId,UserLanguageId);
            if (pm != null)
            {
                TemplatesHelper mpc = new TemplatesHelper();
                TemplateModel mpm= mpc.GetById(pm.MasterPageId);
                SiteResourcesHelper srh = new SiteResourcesHelper();
                SiteResourceModel srm = srh.GetById(mpm.WebResourceId);
                LoadCss(srm.FolderName);
                LoadJavascript(srm.FolderName);
                ltrContent.Text = mpm.Content;
                foreach (PageSections ps in mpm.lstPageSections)
                {
                    ltrContent.Text = ltrContent.Text.Replace("[{" + ps.Name + "}]", GetHTMLForSections(pm.Id,ps.Id,ps.Name));
                }

                BindDropDown();
                Page.Title = pm.Title;
            }


        }
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-1.7.js", ResolveClientUrl("~/Resources/Javascript/jquery-1.7.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery.jixedbar.js", ResolveClientUrl("~/Resources/Javascript/jquery.jixedbar.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-ui-1.8.4.custom.min.js", ResolveClientUrl("~/FileManager/js/jquery-ui-1.8.4.custom.min.js"));
        AddCssLink("FileManager/css/ui-lightness/jquery-ui-1.8.4.custom.css");
    }
    void BindDropDown()
    {
        ModulesHelper mc = new ModulesHelper();
        DDLModule.DataSource= mc.GetAllModules();
        DDLModule.DataTextField = "Name";
        DDLModule.DataValueField = "Id";
        DDLModule.DataBind();
        DDLModule.Items.Insert(0,new ListItem("Select", "0"));
        DDLSubModule.Items.Add(new ListItem("Select", "0"));
    }
    string GetHTMLForSections(int PageId,Guid SectionId,string SectionName)
    {
        PageModulesHelper pm = new PageModulesHelper();
        IEnumerable<PageModuleModel> lstPageModules= pm.GetById(PageId, SectionId,UserLanguageId);
        string ModuleHTML = "";
        foreach (PageModuleModel pmm in lstPageModules)
        {
            ModuleHTML = ModuleHTML + GetModuleDetails(pmm);
        }
        string HTML = "<div class='Section' onmouseover='javascript:showMenu(this);'><input type='hidden' class='hidPageId' value='" + PageId.ToString() + "'></input><input class='hidSectionId' type='hidden' value='" + SectionId.ToString() + "'></input>" + SectionName + "<br/>" + ModuleHTML + "</div>";
        return HTML;
    }
    string GetModuleDetails(PageModuleModel pmm)
    {
        ModulesHelper mc = new ModulesHelper();
        ModuleModel mm = mc.GetById(pmm.ModuleId);

        string TypeName = "";
        string Settings = "";
        if (pmm.SubModuleId > 0)
        {
            SubModule sm = mm.SubModules.Where(c => c.Id == pmm.SubModuleId).FirstOrDefault();
            TypeName = sm.Type;
            Settings = sm.Settings;
        }
        else if (pmm.ModuleId > 0)
        {
            TypeName = mm.Type;
            Settings = mm.Settings;
        }
        Type type = typeof(Modules.IPageModule).Assembly.GetType("Modules." + TypeName + "");
        
        object HTML = type.GetMethod("GetLayoutModeHTML").Invoke(Activator.CreateInstance(type), new object[] { pmm, mm,UserLanguageId });
        return HTML as string;
    }
}